<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
    >
      <el-form-item label="会员名称" prop="inviterId">
          <el-select
              v-model="queryParams.memId"
              filterable
              clearable
              remote
              reserve-keyword
              placeholder="请输入手机号"
              style="width: 200px;"
              :remote-method="remoteMethod"
              :loading="memloading">
              <el-option
                v-for="item in memberOptions"
                :key="item.memId"
                :label="item.realname+'('+item.mobile+')'"
                :value="item.memId">
              </el-option>
            </el-select>
          </el-form-item>
        <el-form-item label="订单状态" prop="status" >
           <el-select v-model="queryParams.orderStatus"  placeholder="请选择订单状态"  clearable  style="width: 200px">
              <el-option
                 v-for="dict in stateObeject"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
              />
           </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="status" >
           <el-select v-model="queryParams.payType"  placeholder="请选择支付方式"  clearable  style="width: 200px">
              <el-option
                 v-for="dict in payObject"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
              />
           </el-select>
        </el-form-item>
        <el-form-item label="支付时间" style="width: 450px;">
            <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
            ></el-date-picker>
          </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Download"
               @click="handleExport"
               v-hasPermi="['memberOrder:index:export']"
            >导出</el-button>
         </el-col>
    </el-row>


    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >

      <el-table-column
        label="订单号"
        width="180"
        prop="orderNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="下单人"
        width="120"
        prop="memName"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="套餐名称"
        align="center"
        prop="packageName"
        width="120"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.packageName||'-'}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="套餐信息"
        align="center"
        prop="createTime"

        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.backupJson.remark ||'-'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单数量"
        align="center"
        prop="buyNum"
        :show-overflow-tooltip="true"
        width="80"
      />
      <el-table-column
        label="订单价格(元)"
        align="center"
        prop="payPrice"
        :show-overflow-tooltip="true"
        width="100"
      />
      <el-table-column
        label="支付方式"
        align="center"
        prop=""
        width="100"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{scope.row.payType == 10?'余额支付':'微信支付'}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="订单状态"
        align="center"
        prop="orderStatusName"
        :show-overflow-tooltip="true"
        width="110"
      />
      <el-table-column
        label="下单时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime) || "-" }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        width="250"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            v-hasPermi="['memberOrder:index:view']"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            link
            type="primary"
            icon="Promotion"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['memberOrder:index:edit']"
            v-if="scope.row.orderStatus == 4"
            >发货</el-button
          >
          <el-button
            link
            type="primary"
            icon="EditPen"
            @click="handleChange(scope.row)"
            v-hasPermi="['memberOrder:index:changeAddress']"
            v-if="scope.row.orderStatus == 4"
            >修改地址</el-button
          >

          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['goods:index:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 发货订单对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <div class="form_box">
        <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
          <!-- <el-row>
            <el-col :span="18"> 订单编号:{{ detailObject.orderNo }} </el-col>
          </el-row> -->
          <!-- <el-row>
            <el-col :span="18"> 订单编号:{{ detailObject.orderNo }} </el-col>
          </el-row> -->
          <el-row>
            <el-col :span="18">
              <el-form-item label="物流单号" prop="expressId">
                <el-input
                  v-model="form.expressId"
                  placeholder="请输入物流单号"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18">
              <el-form-item label="物流公司" prop="expressName">
                <el-input
                  v-model="form.expressName"
                  placeholder="请输入物流公司"
                />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
     <!-- 查看对话框 -->
     <el-dialog :title="detailTitle" v-model="detailOpen" width="800px" append-to-body custom-class="views111">
      <div class="form_box">
        <h3>订单信息</h3>
        <el-descriptions
          class="margin-top"
          :column="2"
          :size="size"
          border
        >
          <el-descriptions-item label="套餐名称">
              {{ detailObject.packageName ||'-'}}
            </el-descriptions-item>

            <el-descriptions-item label="订单数量">
              {{ detailObject.buyNum ||'-'}}
            </el-descriptions-item>
            <el-descriptions-item label="订单价格">
              {{ detailObject.payPrice +'元'||'-'}}
            </el-descriptions-item>
            <el-descriptions-item label="订单号码">
              {{ detailObject.orderNo }}
            </el-descriptions-item>
            <el-descriptions-item label="下单人员">
              {{ detailObject.memName }}
            </el-descriptions-item>
            <el-descriptions-item label="下单时间">
              {{ parseTime(detailObject.createTime) }}
            </el-descriptions-item>

            <el-descriptions-item label="订单状态">
              {{ detailObject.orderStatusName ||'-'}}
            </el-descriptions-item>
            <el-descriptions-item label="付款状态">
              {{ detailObject.payStatusName ||'-'}}
            </el-descriptions-item>
            <el-descriptions-item label="支付时间">
              {{ parseTime(detailObject.payTime) ||'-'}}
            </el-descriptions-item>
            <el-descriptions-item label="支付方式">
              {{ detailObject.payType == 10?'余额支付':'微信支付'}}
            </el-descriptions-item>

            <el-descriptions-item label="套餐信息">
              {{ detailObject.backupJson.remark ||'-'}}
            </el-descriptions-item>


          </el-descriptions>
          <h3>收货信息</h3>
        <el-descriptions
          class="margin-top"
          :column="2"
          :size="size"
          border
        >

            <el-descriptions-item label="收货人员">
              {{  detailObject.addressInfo ?detailObject.addressInfo.name:'-' }}
            </el-descriptions-item>
            <el-descriptions-item label="收货号码">
              {{ detailObject.addressInfo ?detailObject.addressInfo.phone:'-' }}
            </el-descriptions-item>
            <el-descriptions-item label="收货地址" v-if="detailObject.addressInfo">
              {{ detailObject.addressInfo.province_name +detailObject.addressInfo.city_name+detailObject.addressInfo.region_name+detailObject.addressInfo.detail}}
            </el-descriptions-item>



          </el-descriptions>
          <h3 v-if="detailObject.orderExpressId!=0">物流信息</h3>
        <el-descriptions
          class="margin-top"
          :column="2"
          :size="size"
          border
          v-if="detailObject.orderExpressId!=0"
        >
          <el-descriptions-item label="物流公司">
            {{  detailObject.orderExpressName ||'-' }}
          </el-descriptions-item>
          <el-descriptions-item label="物流单号">
            {{  detailObject.orderExpressId!=0?detailObject.orderExpressId:'-' }}
          </el-descriptions-item>
          <el-descriptions-item label="发货时间">
            {{  detailObject.orderExpressTime ||'-' }}
          </el-descriptions-item>
          <el-descriptions-item label="收货时间">
            {{  detailObject.orderExpressReTime ||'-' }}
          </el-descriptions-item>
          <el-descriptions-item label="物流状态">
            {{  detailObject.orderExpressStatusName ||'-' }}
          </el-descriptions-item>
          </el-descriptions>
      </div>

    </el-dialog>
       <!-- 修改地址对话框 -->
    <el-dialog :title="changeTitle" v-model="changeOpen" width="800px" append-to-body>
      <div class="form_box">
        <el-form ref="orderChangeRef" :model="changeForm" :rules="changeRules" label-width="80px">
          <!-- <el-row>
            <el-col :span="18"> 订单编号:{{ detailObject.orderNo }} </el-col>
          </el-row> -->
          <!-- <el-row>
            <el-col :span="18"> 订单编号:{{ detailObject.orderNo }} </el-col>
          </el-row> -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="收件人" prop="name">
                <el-input
                  v-model="changeForm.name"
                  placeholder="请输入收件人"
                />
              </el-form-item>
            </el-col>


            <el-col :span="12">
              <el-form-item label="手机号" prop="phone">
                <el-input
                  v-model="changeForm.phone"
                  placeholder="请输入手机号"

                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="省份" prop="province_name">
                <el-input
                  v-model="changeForm.province_name"
                  placeholder="请输入省份"
                />
              </el-form-item>
            </el-col>


            <el-col :span="12">
              <el-form-item label="城市" prop="city_name">
                <el-input
                  v-model="changeForm.city_name"
                  placeholder="请输入城市"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="区\县" prop="region_name">
                <el-input
                  v-model="changeForm.region_name"
                  placeholder="请输入区\县"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="街道" prop="street_name">
                <el-input
                  v-model="changeForm.street_name"
                  placeholder="请输入街道"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="详细地址" prop="detail">
                <el-input
                  v-model="changeForm.detail"
                  placeholder="请输入详细地址"
                  type="textarea"
                />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitChangeForm">确 定</el-button>
          <el-button @click="changeCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
      <!-- <template #footer v-if="detailObject.orderStatus == 4">
        <div class="dialog-footer">
          <el-button type="primary" @click="handleUpdate">发 货</el-button>

        </div>
      </template> -->

  </div>
</template>

<script setup name="memberOrder">
import { getMemberActiveList, sendOutmember, getOrderDetail,memberActiveExport,editAddressInfo } from "@/api/memberOrder/list";
import { getMemberList} from "@/api/member/index";
const { proxy } = getCurrentInstance();

const orderList = ref([]);
const skuList = ref([]);
const open = ref(false);
const loading = ref(true);

const memberOptions = ref([]);
const memloading = ref(false);

const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("发货");
const detailTitle = ref("订单详情");
const detailOpen = ref(false);
const dateRange = ref([]);
const stateObeject = ref([

  {
    label:'待付款',
    value:2
  },
  {
    label:'待发货',
    value:4
  },
  {
    label:'待收货',
    value:5
  },
  {
    label:'已完成',
    value:6
  },
  {
    label:'已取消',
    value:9
  }
])

const payObject = ref([

  {
    label:'余额支付',
    value:10
  },
  {
    label:'微信支付',
    value:20
  }
])
const changeTitle = ref("修改地址");
const changeOpen = ref(false);
const data = reactive({
  form: {},
  changeForm: {},
  detailObject: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: undefined,
    orderStatus: '',
    memId:undefined,
    payType:undefined
  },

  rules: {
    expressName: [
      { required: true, message: "物流公司不能为空", trigger: "blur" },
    ],
    expressId: [
      { required: true, message: "物流单号不能为空", trigger: "blur" },
    ],
  },
  changeRules:{
    expressName: [
      { required: true, message: "物流公司不能为空", trigger: "blur" },
    ],
    expressId: [
      { required: true, message: "物流单号不能为空", trigger: "blur" },
    ],
  }
});

const { queryParams, form, rules, detailObject,changeForm,changeRules } = toRefs(data);

/** 查询订单列表 */
function getList() {
  loading.value = true;
  getMemberActiveList(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}


/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    expressId: undefined,
    expressName: undefined,
  };
  proxy.resetForm("orderRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.memId = undefined;
  queryParams.value.orderStatus = '';
  queryParams.value.payType = undefined;
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 查看订单详情 */
function handleView(row){
  detailObject.value = row
  detailOpen.value = true
}
/** 发货按钮操作 */
function handleUpdate(row) {
  reset();
  detailObject.value = row;
  form.value.id = row.id;
  open.value = true;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate((valid) => {
    if (valid) {
      sendOutmember(form.value).then((response) => {
        proxy.$modal.msgSuccess("发货成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const goodsId = row.goodsId;
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.goodsName + '"的数据项？')
    .then(function () {
      return deleteGoods(goodsId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}



function addDateRanges(params, dateRange, propName) {
  let search = params;
  search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  dateRange = Array.isArray(dateRange) ? dateRange : [];
  if (typeof (propName) === 'undefined') {
    search.params['beginTime'] = dateRange[0];
    search.params['endTime'] = dateRange[1];
  } else {
    search.params['begin' + propName] = dateRange[0];
    search.params['end' + propName] = dateRange[1];
  }
  delete search.pageNum;
  delete search.pageSize;
  return search;
}
/** 导出按钮操作 */
function handleExport() {

  memberActiveExport(addDateRanges(queryParams.value, dateRange.value)).then((response) => {
    window.location.href = response.data
  });
}
function remoteMethod(query) {
  if (query !== '') {
    memloading.value = true;
    setTimeout(() => {

      getMemberList({mobile:query}).then(response => {
        memberOptions.value = response.data
        memloading.value = false;
      });

    }, 200);
  } else {
    memberOptions.value = [];
  }
}
/** 取消按钮 */
function changeCancel() {
  changeOpen.value = false;
  changeReset();
}

/** 表单重置 */
function changeReset() {
  changeForm.value = {
    order_id: undefined,
    name: undefined,
    phone: undefined,
    province_name: undefined,
    city_name: undefined,
    region_name: undefined,
    street_name: undefined,
    detail: undefined,
  };
  proxy.resetForm("orderChangeRef");
}

function handleChange(row){
  changeReset()
  if(row.addressInfo){
    changeForm.value = row.addressInfo
  }

  changeForm.value.order_id = row.id
  changeOpen.value = true
}

function submitChangeForm(){
  proxy.$refs["orderChangeRef"].validate((valid) => {
    if (valid) {
      editAddressInfo(changeForm.value).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        changeOpen.value = false;
        getList();
      });
    }
  });
}
getList();
</script>
<style>
  .views111{
    .el-dialog__body{
      padding-top: 0;
    }
  }
</style>
<style lang="scss" scoped>

.order_items {
  display: flex;
  align-items: center;
  .order_item_right {
    display: flex;
    flex-direction: column;
  }
  .word_over {
    width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.detail_content {
  height: 60vh;
  overflow: auto;
}
.detail_title {
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 30px;
}
.price {

}
.order_item {
  display: flex;
  flex-direction: column;
}
.order_items {
  display: flex;
  align-items: center;
  .order_item_right {
    display: flex;
    flex-direction: column;
  }
}
.detail_box {
  display: flex;
  flex-direction: column;
}

.detail_title {
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 30px;
}
.detail_item:last-child{
  border-bottom: none;
}
.detail_item {
  width: 100%;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #dddd;
  padding: 10px;
  box-sizing: border-box;
  .detail_middle {
    margin-left: 10px;
    display: flex;
    align-items: center;

    justify-content: space-between;
  }
  .detail_middles {
    width: 450px;
    display: flex;
    flex-direction: column;

    justify-content: space-between;
    .name {
      color: #606266;
      font-size: 14px;
    }
    .no {
      margin-top: 12px;
      font-size: 12px;
      color: #aaa;
    }
  }
  .detail_right {
    width: 100px;
    margin-left: 50px;
    display: flex;

  }
}
</style>
